package com.liu.voluntary.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.voluntary.service.UserService;
import com.liu.voluntary.mapper.UserMapper;
import com.liu.voluntary.pojo.User;
import com.liu.voluntary.util.CookieUtil;
import com.liu.voluntary.util.MD5Util;
import com.liu.voluntary.util.UUIDUtil;
import com.liu.voluntary.util.ValidatorUtil;
import com.liu.voluntary.vo.RespBean;
import com.liu.voluntary.vo.RespBeanEnum;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Resource
    private UserMapper userMapper;
    @Resource
    private RedisTemplate redisTemplate;


    @Override
    public RespBean login(String mobile, String password, HttpServletRequest request, HttpServletResponse response) {


        //判断手机号和密码是否为空
        if (!StringUtils.hasText(mobile) || !StringUtils.hasText(password)) {
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }

        //校验手机号是否符合所定的正则表达式
        //手机号错误
        if (!ValidatorUtil.isMobile(mobile)) {
            return RespBean.error(RespBeanEnum.MOBILE_ERROR);
        }

        //查询数据库，看用户是否存在
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);

//        List<User> users = userMapper.selectList(wrapper);

        User user = userMapper.selectOne(wrapper);
        if (user == null) {
            return RespBean.error(RespBeanEnum.MOBILE_NOT_EXIST);
        }

        //此时的password为用户输入的密码
        //密码不正确
        if (!MD5Util.inputPassToDBPass(password).equals(user.getPassword())) {
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }


        //给每个用户生成一个唯一的ticket
        String ticket = UUIDUtil.uuid();

        //将登录成功的用户保存到session
        request.getSession().setAttribute(ticket, user);

        redisTemplate.opsForValue().set("user:"+ticket,user);

        //将ticket保存到cookie
        CookieUtil.setCookie(request, response, "Ticket", ticket);

        //登录成功
        return RespBean.success(ticket);
    }

    @Override
    public RespBean register(String mobile, String password, HttpServletRequest request, HttpServletResponse response) {

        if (!ValidatorUtil.isMobile(mobile)) {
            return RespBean.error(RespBeanEnum.MOBILE_ERROR);
        }

        //查询数据库，看用户是否存在

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);

        User user = userMapper.selectOne(wrapper);
        if (user != null) {
            return RespBean.error(RespBeanEnum.REGISTER_REPEAT);
        }



        return RespBean.success();

    }



    @Override
    public User getUserInRedis(String Ticket, HttpServletRequest request, HttpServletResponse response) {

        if(!StringUtils.hasText(Ticket)){

            try {
                response.sendRedirect("/toLoginPage");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return null;
        }

        User user = (User) redisTemplate.opsForValue().get("user:" + Ticket);

        //如果用户不为空,刷新Cookie，刷新了生命周期
        if (user != null){
            CookieUtil.setCookie(request,response,"Ticket",Ticket);
        }

        return user;
    }





}
